import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'

export default {
  namespace: 'recruitmentRelease',
  state: {

    districtTypeRange: [],
    typeList: [
      {
        value: 0,
        label: '求职'
      }, {
        value: 1,
        label: '招聘'
      }
    ],
    // 条件
    districtType: {
      id: 0,
      name: '不限'
    },
    nowType: 0,
    contact: "",
    cellPhone: "",
    description: "",
    title: "",
    endData: "",
    isOpened: false,
  },
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    },
    initData(state, action) {
      return {
        ...state,
        ...action.payload,
        nowType: 0,
        districtType: {
          id: 0,
          name: '不限'
        },
        contact: "",
        cellPhone: "",
        title: "",
        description: "",
        endData: "",
      }
    }
  },
  effects: {
    // 获得区域信息
    * loadDistrict({payload = {}}, {put, call}) {
      const {data} = yield call(request, {
        url: SERVERURL + '/service/city/all',
        method: 'get',
      })
      if (data && data.data instanceof Array) {
        const d = data.data || {};
        yield put({
          type: 'updateState',
          payload: {
            districtTypeRange: d
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    //添加
    * addRecruitment({payload = {}}, {select, call}) {
      const {
        nowType, districtType, contact, cellPhone, title, description, endData
      } = yield select(({recruitmentRelease}) => recruitmentRelease)
      let districtId = districtType.id;
      let type = nowType;
      if (contact == "" || cellPhone == "" || title == "" || description == "" || endData == "") {
        Taro.showToast({
          title: "信息未填写完整！",
          icon: 'none'
        })
        return false
      }
      if (!(/^1[3456789]\d{9}$/.test(cellPhone))) {
        Taro.showToast({
          title: "手机号码有误，请重填！",
          icon: 'none'
        })
        return false;
      }
      const param = {
        contact, cellPhone, description, title, districtId, type, endData
      }
      let url = 'service/recruit/add';

      const {data} = yield call(request, {
        url: SERVERURL + url,
        data: param,
        method: 'post',
      })
      if (data && data === "unauthc") {
        Taro.navigateTo({
          url: `/pages/login/index`
        })
        Taro.showToast({
          title: ("会话过时"),
          icon: 'none'
        })
      }
      if (data && data.code === '000000') {
        Taro.redirectTo({
          url: `/pages/businessHome/index`
        })
        Taro.showToast({
          title: (data || {}).msg || '添加成功',
          icon: 'none'
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '添加失败',
          icon: 'none'
        })
      }
    }
  }
};
